package com.dkm.orproject.dkmoa.action.flow;

import com.alibaba.fastjson.JSONObject;
import com.dkm.orproject.dkmoa.domain.OaEntryFamily;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.service.flow.OaEntryFamilyService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: orproject
 * @DESCRIPTION:
 * @USER: 周佳佳
 * @DATE: 2020/8/11 13:56
 */
@RestController
@RequestMapping("/oaEntryFamily")
public class OaEntryFamilyAction {
    @Autowired
    OaEntryFamilyService oaEntryFamilyService;

    @Resource
    Tools tools;

    /**
     * 根据人员信息id查询家庭成员
     * @param request 请求
     * @param entryId 流程id
     * @return 返回数据
     */
    @UserLoginToken
    @GetMapping("/selectAll")
    public Map<String,Object> findAll(HttpServletRequest request, Long entryId){
        try {
            //分页查询集合
            List<OaEntryFamily> logs=oaEntryFamilyService.selectAll(entryId);
            //返回集合
            return Tools.maps(logs,0L);
        } catch (Exception e) {
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogIp(request.getRemoteAddr());
            log.setLogTitle("根据人员信息id查询家庭成员的异常模块");
            log.setLogType("0");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 根据人员信息id查询家庭成员
     * @param request 请求
     * @param entryId 流程id
     * @return 返回数据
     */
    @UserLoginToken
    @GetMapping("/selectAlls")
    public Map<String,Object> findAlls(HttpServletRequest request, Long entryId){
        try {
            //分页查询集合
            List<OaEntryFamily> logs=oaEntryFamilyService.selectAlls(entryId);
            //返回集合
            return Tools.maps(logs,0L);
        } catch (Exception e) {
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogIp(request.getRemoteAddr());
            log.setLogTitle("根据人员信息id查询家庭成员的异常模块");
            log.setLogType("0");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            tools.errorMap(map,log);
            return map;
        }
    }

    @UserLoginToken
    @PostMapping("addOrUpdate")
    public Map<String,Object> addOrUpdate(OaEntryFamily entryFamily, HttpServletRequest request){
        OaPersonnel user = (OaPersonnel) request.getSession().getAttribute("user");

        Map<String, Object> map = new HashMap<>();
        if(ObjectUtils.isEmpty(user)){
            tools.rowMap(map,0,null);
        }
        try {
            int rows=0;
            OaLog log = new OaLog();
            //如果id不为空 那么就修改 否则增加
            if(entryFamily.getPkFamilyId()!=null &&!"".equals(entryFamily.getPkFamilyId())){
                //传入更新人
                entryFamily.setUpdatedBy(user.getPersonnelName());
                //传入更新时间
                entryFamily.setUpdatedTime(new Date());
                log.setLogTitle("家庭成员修改模块");
                rows = oaEntryFamilyService.updateByPrimaryKeySelective(entryFamily);
            }else{
                entryFamily.setCreatedBy(user.getPersonnelName());
                log.setLogTitle("家庭成员增加模块");
                entryFamily.setIsRevision("1");
                rows = oaEntryFamilyService.insertSelective(entryFamily);
            }
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            log.setCreatedBy(user.getPersonnelName());
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            log.setLogTitle("家庭成员增加或修改模块异常");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(user.getPersonnelName());
            tools.errorMap(map, log);
            return map;
        }
    }


    @UserLoginToken
    @PostMapping("addOrUpdates")
    public Map<String,Object> addOrUpdates(OaEntryFamily entryFamily, HttpServletRequest request){
        OaPersonnel user = (OaPersonnel) request.getSession().getAttribute("user");

        Map<String, Object> map = new HashMap<>();
        if(ObjectUtils.isEmpty(user)){
            tools.rowMap(map,0,null);
        }
        try {
            int rows=0;
            OaLog log = new OaLog();
            //如果id不为空 那么就修改 否则增加
            if(entryFamily.getPkFamilyId()!=null &&!"".equals(entryFamily.getPkFamilyId())){
                //传入更新人
                entryFamily.setUpdatedBy(user.getPersonnelName());
                //传入更新时间
                entryFamily.setUpdatedTime(new Date());
                log.setLogTitle("家庭成员修改模块");
                rows = oaEntryFamilyService.updateByPrimaryKeySelective(entryFamily);
            }else{
                entryFamily.setCreatedBy(user.getPersonnelName());
                log.setLogTitle("家庭成员增加模块");
                entryFamily.setIsRevision("2");
                rows = oaEntryFamilyService.insertSelective(entryFamily);
            }
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            log.setCreatedBy(user.getPersonnelName());
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            log.setLogTitle("家庭成员增加或修改模块异常");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(user.getPersonnelName());
            tools.errorMap(map, log);
            return map;
        }
    }

    /**
     * 删除控制器
     * @param pkFamilyId 主键
     * @param session  登录人信息
     * @param request 请求
     * @return 返回数据
     */
    @UserLoginToken
    @DeleteMapping("delete/{pkFamilyId}")
    public Map<String, Object> delete(@PathVariable("pkFamilyId") Long pkFamilyId, HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        try {
            OaLog log = new OaLog();
            OaEntryFamily entryFamily=new OaEntryFamily();
            entryFamily.setPkFamilyId(pkFamilyId);
            entryFamily.setIsRevision("0");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogTitle("家庭成员的删除模块");
            int rows= oaEntryFamilyService.updateByPrimaryKeySelective(entryFamily);
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            log.setLogIp(request.getRemoteAddr());
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("家庭成员的删除模块异常");
            log.setCreatedBy(user.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }
    /**
     * 日期格式化 因为前端传的日期是String 后端模型接受的是Date
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }
}
